package com.ktjy.lot.config.emqx;


import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/*
    通过发送http请求获取mqtt服务器中设备情况
    curl -u 7ff53d200c5aa3eb:owBcBUXTPwZIOMmR9ALPwlEHdSFuXXcJ4dD8CL6mz6lB http://110.41.18.202:18083/api/v5/clients
 */
@Component
public class EmqxApiClient {
    private String baseUrl;
    private HttpClient client;
    //private final String host;
    private final String username;
    //private final int port;
    private final String password;

    public EmqxApiClient(@Value("${emqx.host}") String host,
                         @Value("${emqx.port}") int port,
                         @Value("${emqx.username}") String username,
                         @Value("${emqx.password}") String password) {
        this.username = username;
        this.password = password;
        this.baseUrl = String.format("http://%s:%d/api/v5", host, port);
        this.client = HttpClient.newBuilder().build();
    }

//    public EmqxApiClient(String host, int port, String username, String password) {
//        this.baseUrl = String.format("http://%s:%d/api/v5", host, port);
//        this.username = username;
//        this.password = password;
//        this.client = HttpClient.newBuilder().build();
//    }

    // 获取所有在线客户端
    public JSONArray getOnlineClients() throws Exception {
        String authHeader = "Basic " + Base64.getEncoder().encodeToString(
                (username + ":" + password).getBytes(StandardCharsets.UTF_8));
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(baseUrl + "/clients"))
                .header("Authorization", authHeader)
                .GET()
                .build();
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        if (response.statusCode() == 200) {
            JSONObject result = JSONObject.parseObject(response.body());
            return result.getJSONArray("data");
        } else {
            throw new Exception("Failed to get clients: " + response.body());
        }
    }

    // 获取指定客户端详情
    public JSONObject getClientDetail(String clientId) throws Exception {
        String authHeader = "Basic " + Base64.getEncoder().encodeToString(
                (username + ":" + password).getBytes(StandardCharsets.UTF_8));
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(baseUrl + "/clients/" + clientId))
                .header("Authorization", authHeader)
                .GET()
                .build();
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        if (response.statusCode() == 200) {
            return JSONObject.parseObject(response.body());
        } else {
            throw new Exception("Failed to get client detail: " + response.body());
        }
    }
}
